'use strict';

const Controller = require('egg').Controller;

class RoleController extends Controller {
  async index() {
    const { ctx, app } = this;
    let option = ctx.helper.formatOptions(ctx.query, ["roleName", "roleKey", "status"]);
    option.order = app.Sequelize.col("roleSort");
    try {
      let result = await ctx.model.SysRole.findAndCountAll(option);
      ctx.body = ctx.helper.formateResponseList(result);
    } catch (error) {
      ctx.body = ctx.helper.formateResponse(null, 500, "查询失败")
    }
  }
  async changeStatus() {
    const { ctx } = this;
    const params = ctx.request.body;
    const currentTarget = await ctx.model.SysRole.findByPk(Number(params.roleId));
    if (currentTarget) {
      const role = currentTarget.toJSON();
      const status = role.status === "1" ? "0" : "1";
      const result = await currentTarget.update({ status });
      ctx.body = ctx.helper.formateResponse(result);
    } else {
      ctx.body = ctx.helper.formateResponse(null, 500, "roleId有误");
    }
  }
  async show() {
    const { ctx } = this;
    const { id } = ctx.params;
    const currentTarget = await ctx.model.SysRole.findByPk(Number(id));
    if (currentTarget) {
      ctx.body = ctx.helper.formateResponse(currentTarget);
    } else {
      ctx.body = ctx.helper.formateResponse(null, 500, "roleId有误");
    }
  }
  async create() {
    const { ctx, app } = this;
    const { Op } = app.Sequelize;
    const data = ctx.request.body;
    const menus = await ctx.model.SysMenu.findAll({
      where: {
        menuId: {
          [Op.or]: data.menuIds
        }
      }
    });
    try {
      await ctx.model.transaction(async t => {
        const role = {
          dept_check_strictly: data.deptCheckStrictly ? 1 : 0,
          menu_check_strictly: data.menuCheckStrictly ? 1 : 0,
          remark: data.remark,
          roleKey: data.roleKey,
          roleName: data.roleName,
          roleSort: data.roleSort,
          status: data.status
        }
        const result = await ctx.model.SysRole.create(role, { transaction: t });
        return await result.setSys_menus(menus, { transaction: t });
      });

      ctx.body = ctx.helper.formateResponse(null);
    } catch (error) {
      ctx.body = ctx.helper.formateResponse(error, 500, 'fail');
    }
  }
  async update() {
    const { ctx, app } = this;
    const { Op } = app.Sequelize;
    const updateV = ctx.request.body;
    try {
      await ctx.model.transaction(async t => {
        let oldV = await ctx.model.SysRole.findOne({
          where: {
            roleId: updateV.roleId
          },
          include: [ctx.model.SysMenu]
        }, { transaction: t });

        let n_menus = [];
        if(updateV.menuIds.length){
          n_menus = await ctx.model.SysMenu.findAll({
            where: {
              menuId: {
                [Op.or]: updateV.menuIds
              }
            }
          }, { transaction: t });
        }
        
        let role = {
          dept_check_strictly: updateV.deptCheckStrictly ? 1 : 0,
          menu_check_strictly: updateV.menuCheckStrictly ? 1 : 0,
          remark: updateV.remark,
          roleKey: updateV.roleKey,
          roleName: updateV.roleName,
          roleSort: updateV.roleSort,
          status: updateV.status
        }

        await oldV.update(role, { transaction: t });
        return await oldV.setSys_menus(n_menus, { transaction: t });
      });
      ctx.body = ctx.helper.formateResponse(null);
    } catch (error) {
      console.log(error,'err');
      ctx.body = ctx.helper.formateResponse(null,500,error);
    }
  }
  async dataScope(){
    const {ctx,app} = this;
    const data = ctx.request.body;
    const {dataScope,deptIds,deptCheckStrictly} = data;
    const { Op } = app.Sequelize;
    const role = await ctx.model.SysRole.findByPk(Number(data.roleId));
    await role.update({dataScope,dept_check_strictly:deptCheckStrictly});
    if(dataScope !== "2"){
      await role.setSys_depts([])
    }else{
      let deptLists = [];
      if(deptIds.length){
        deptLists = await ctx.model.SysDept.findAll({
          where:{
            deptId:{
              [Op.or]: deptIds
            }
          }
        });
      }
      console.log(deptIds,deptLists.length,88888888);
      await role.setSys_depts(deptLists);
    }

    ctx.body = ctx.helper.formateResponse(null);
  }
  async destroy() {
    const ctx = this.ctx;
    const id = ctx.params.id;
    const result = await ctx.model.SysRole.findByPk(Number(id));
    if (result) {
      await result.setSys_menus([]);
      await result.setSys_depts([]);
      await result.destroy();
    } else {
      ctx.body = ctx.helper.formateResponse(null, 500, "删除失败");
      return
    }
    ctx.body = ctx.helper.formateResponse(null);
  }

}

module.exports = RoleController;
